package com.yxp.offer;

import com.yxp.middle.Problem1008;
import com.yxp.util.TreeNode;

/**
 * @author : yang.xp
 * @date : 2021/8/19 10:20
 * @description : 剑指 Offer 28. 对称的二叉树
 * 请实现一个函数，用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样，那么它是对称的。
 */
public class Problem28 {

    public static void main(String[] args) {
        int[] numbers = new int[]{1, 2, 2, 5, 3, 5, 3};
        Problem1008 problem1008 = new Problem1008();
        Problem28 problem28 = new Problem28();
        System.out.println(problem28.isSymmetric(problem1008.bstFromPreorder(numbers)));
    }

    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        return dfs(root.left, root.right);
    }

    public boolean dfs(TreeNode A, TreeNode B) {
        if (A == null && B == null) return true;
        if (A == null || B == null || A.val != B.val) return false;
        return dfs(A.left, B.right) && dfs(A.right, B.left);
    }
}
